// pages/payHouseDeatil/payHouseDeatil.js
var QQMapWX = require('../../utils/qqmap-wx-jssdk1.2/qqmap-wx-jssdk.js');
var myPlugin = requirePlugin('WechatSI');  // 引入插件
// 实例化API核心类
var qqmapsdk = new QQMapWX({
    key: 'RNOBZ-EQR6R-UN6W5-WDY6A-FEJI6-2MBIV' // 必填
});
Page({

  /**
   * 页面的初始数据
   */
  data: {
        detailInfo:'',
        markers:[{
          latitude:'1',
          longitude:''
        }],
        latitude:'',
        longitude:'',
        navigation:{}
  },

  /**
   * 生命周期函数--监听页面加载
   */
 
  onLoad(options) {
      wx.getLocation({
      altitude: 'altitude',
      success:res=>{
       // console.log(res)
        this.setData({
          latitude:res.latitude,
          longitude:res.longitude
        })
      }
    })
      var detail_id=options.detail_id
      wx.request({
        url: 'http://www.house.com/index.php/api/payHouseDetail',
        data:{
          detail_id
        },
        success:res=>{
          if(res.data.code==200){
            this.setData({
              detailInfo:res.data.data,
              "markers[0].latitude":res.data.data.house_latitude,
              "markers[0].longitude":res.data.data.house_longitude
            })
            wx.setStorageSync('brokerImg', res.data.data.house_broker.broker_image)
          }
        }
      })
  },

  /**
   * 导航+语音播报
   * @param {*} e 
   */
  navigation(e){
    //获取我的位置 经纬度
    let myLatitude = this.data.latitude;
    let myLongitude = this.data.longitude;
    let myLocation = myLatitude+','+myLongitude
    //获取房源位置经纬度
    let houseLatitude = this.data.detailInfo.house_latitude
    let houseLongitude = this.data.detailInfo.house_longitude
    let houseLocation = houseLatitude+','+houseLongitude;
    
    var _this = this;
    //调用距离计算接口
    qqmapsdk.direction({
      mode: 'driving',//可选值：'driving'（驾车）、'walking'（步行）、'bicycling'（骑行），不填默认：'driving',可不填
      //from参数不填默认当前地址  我的位置
      from: myLocation,
      to: houseLocation, 
      success: function (res) {
        //console.log(res);
        var ret = res;
        var coors = ret.result.routes[0].polyline, pl = [];
        //坐标解压（返回的点串坐标，通过前向差分进行压缩）
        var kr = 1000000;
        for (var i = 2; i < coors.length; i++) {
          coors[i] = Number(coors[i - 2]) + Number(coors[i]) / kr;
        }
        //将解压后的坐标放入点串数组pl中
        for (var i = 0; i < coors.length; i += 2) {
          pl.push({ latitude: coors[i], longitude: coors[i + 1] })
        }
        console.log(pl)
        //设置polyline属性，将路线显示出来,将解压坐标第一个数据作为起点
        _this.setData({
          latitude:pl[0].latitude,
          longitude:pl[0].longitude,
          polyline: [{
            points: pl,
            color: '#FF0000DD',
            width: 4
          }]
        })
      },
 
      fail: function (error) {
        console.error(error);
      },
      complete:res=>{
        this.innerAudioContext = wx.createInnerAudioContext();
        let path = res.result.routes[0].steps;
       let info=[];
        path.forEach((value=>{
         // this.info = this.info+value.instruction
         info.push(value.instruction)
       
        //value.instruction
        }));
        let finalInfo = info.toString()
        console.log(finalInfo)
        myPlugin.textToSpeech({   // 调用插件的方法
        lang: 'zh_CN',
        content:finalInfo,
        success:res=>{
          console.log(res)
          this.playAudio(res.filename)
        },
      })
      }
    });
  },
  playAudio(e){
    this.innerAudioContext.src = e
    this.innerAudioContext.play();
 },
/**
 * 选择导航方式
 */
checkNavigationType(){
  
 }

})